package com.application.magazine;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.application.magazine.util.DateUtil;

import android.app.Activity;
import android.util.Log;

public class JSONParserForGetList {
	public Activity a;
	public String news_cat;
	// constructor
	public JSONParserForGetList() {

	}


	public ArrayList<Item_Magazine> getMagazineAll() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		JSONArray json = getMagazinefromURL(GlobalVariable.URL_MAGAZINE_ALL,nameValuePairs);
		ArrayList<Item_Magazine> magList = new ArrayList<Item_Magazine>();
		if (json != null) {
			JSONArray one = json;

			try {
				List<String> imgList = null;
				for (int i = 0; i < one.length(); i++) {
					Item_Magazine mag = new Item_Magazine();
					JSONObject json_data = one.optJSONObject(i);
					JSONObject mag_data = json_data.getJSONObject("Magazine");
					mag.setId(mag_data.getString("id"));
					mag.setLink(mag_data.getString("thumbnail_url"));
					mag.setPdfURL(mag_data.getString("pdf_url"));
					mag.setTitle(mag_data.getString("title"));
					Date pubDate = DateUtil.convertStringToDate(mag_data.getString("publish_date"));
					mag.setPubdate(DateUtil.toStringEngDateSimpleFormat(pubDate));
					mag.setDesc(mag_data.getString("summary"));
					mag.setCategory_id(mag_data.getString("category_id"));
					mag.setDownload_count(mag_data.getString("download_count"));
					
					imgList = new ArrayList<String>();;
					JSONObject imgs =  mag_data.getJSONObject("pdf_images");

					for(int j = 1; j <= imgs.length(); j++){
						imgList.add(imgs.getString("page-"+j));
					}
					mag.setImg_paths(imgList);
					magList.add(mag);
				}
			} catch (Exception e) {
				Log.e("log_tag", "Error parsing data " + e.toString());
			}

		} else {
			return null;
		}
		return magList;
	}
	public ArrayList<Item_Magazine> getMagazineLast2() {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		 nameValuePairs.add(new BasicNameValuePair("category_id",""+7));
		JSONArray json = getMagazinefromURL(GlobalVariable.URL_MAGAZINE_ALL,nameValuePairs);
		ArrayList<Item_Magazine> magList = new ArrayList<Item_Magazine>();
		if (json != null) {
			JSONArray one = json;

			try {
				List<String> imgList = null;
				for (int i = 0; i < one.length(); i++) {
					Item_Magazine mag = new Item_Magazine();
					JSONObject json_data = one.optJSONObject(i);
					JSONObject mag_data = json_data.getJSONObject("Magazine");
					mag.setId(mag_data.getString("id"));
					mag.setLink(mag_data.getString("thumbnail_url"));
					mag.setPdfURL(mag_data.getString("pdf_url"));
					mag.setTitle(mag_data.getString("title"));
					Date pubDate = DateUtil.convertStringToDate(mag_data.getString("publish_date"));
					mag.setPubdate(DateUtil.toStringEngDateSimpleFormat(pubDate));
					mag.setDesc(mag_data.getString("summary"));
					mag.setCategory_id(mag_data.getString("category_id"));
					mag.setDownload_count(mag_data.getString("download_count"));
					
					imgList = new ArrayList<String>();;
					JSONObject imgs =  mag_data.getJSONObject("pdf_images");

					for(int j = 1; j <= imgs.length(); j++){
						imgList.add(imgs.getString("page-"+j));
					}
					mag.setImg_paths(imgList);
					magList.add(mag);
				}
			} catch (Exception e) {
				Log.e("log_tag", "Error parsing data " + e.toString());
			}

		} else {
			return null;
		}
		return magList;
	}
//	public ArrayList<Item_News> getNewsAll(Activity a) throws Exception {
//		this.a = a;
//		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//		JSONArray json = getNewsByRss("http://camt-science.agter-technology.co.th/rss2json.php?url" +
//				"=www.dpe.go.th/rss/news/dpe_adv_news.xml",nameValuePairs);
//		ArrayList<Item_News> newList = new ArrayList<Item_News>();
////		if (json != null) {
//			JSONArray one = json;
//			try {
//				 RSSReader reader = new RSSReader();
//				  String uri = "http://www.dpe.go.th/rss/news/dpe_adv_news.xml";
//				  RSSFeed feed = reader.load(uri);
//				  List<RSSItem> list = feed.getItems();
//				  for (RSSItem i: list) {
//					Item_News news = new Item_News();
//				    news.setTitle(i.getTitle());
//				    news.setLink(i.getLink().toString());
//				    news.setDesc(getImageSrc(i.getDescription()));
//				    news.setCategory(a.getResources().getString(R.string.news_adv));
//				    newList.add(news);
//				  }
//			} catch (Exception e) {
//				Log.e("log_tag", "Error parsing data " + e.toString());
//			}
//
////		} else {
////			return null;
////		}
//		return newList;
//	}
//	public ArrayList<Item_News> getNews(Activity a,String type) throws Exception {
//		this.a = a;
//		
//		
//		ArrayList<Item_News> newList = new ArrayList<Item_News>();
//			try {
//				 RSSReader reader = new RSSReader();
//				  String uri = getUriNews(type);
//				  RSSFeed feed = reader.load(uri);
//				  List<RSSItem> list = feed.getItems();
//				  for (RSSItem i: list) {
//					Item_News news = new Item_News();
//				    news.setTitle(i.getTitle());
//				    news.setLink(i.getLink().toString());
//				    news.setDesc(getImageSrc(i.getDescription()));
//				    news.setCategory(news_cat);
//				    news.setPubdate(calDiffDate(i.getPubDate()));
//				    newList.add(news);
//				  }
//			} catch (Exception e) {
//				Log.e("log_tag", "Error parsing data " + e.toString());
//			}
//		return newList;
//	}
	
//	public List<Item_News> getAllNews(Activity a,int date_type) throws Exception {
//		this.a = a;
//		List<Item_News> newList = new ArrayList<Item_News>();
//			try {
//				 RSSReader reader = new RSSReader();
//				switch (date_type) {
//				case 1:
//					
//					 for(int i=1;i<10;i++){
//					  String uri = getUriNews(String.valueOf(i));
//					  RSSFeed feed = reader.load(uri);
//					  List<RSSItem> list = feed.getItems();
//					  for (RSSItem r: list) {
//						if(getDiffDay(r.getPubDate())<14){  
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//						}
//					  }
//					 }
//					break;
//				case 2:
//					 for(int i=1;i<10;i++){
//					  String uri = getUriNews(String.valueOf(i));
//					  RSSFeed feed = reader.load(uri);
//					  List<RSSItem> list = feed.getItems();
//					  for (RSSItem r: list) {
//						if(getDiffDay(r.getPubDate())<30){  
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//						}
//					  }
//					 }
//				break;
//				case 3:
//					 for(int i=1;i<10;i++){
//					  String uri = getUriNews(String.valueOf(i));
//					  RSSFeed feed = reader.load(uri);
//					  List<RSSItem> list = feed.getItems();
//					  for (RSSItem r: list) {
//						if(getDiffDay(r.getPubDate())<90){  
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//						}
//					  }
//					 }
//				break;
//				case 4:
//					 for(int i=1;i<10;i++){
//					  String uri = getUriNews(String.valueOf(i));
//					  RSSFeed feed = reader.load(uri);
//					  List<RSSItem> list = feed.getItems();
//					  for (RSSItem r: list) {
//						if(getDiffDay(r.getPubDate())<180){  
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//						}
//					  }
//					 }
//				break;
//				case 5:
//					 for(int i=1;i<10;i++){
//					  String uri = getUriNews(String.valueOf(i));
//					  RSSFeed feed = reader.load(uri);
//					  List<RSSItem> list = feed.getItems();
//					  for (RSSItem r: list) { 
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//						
//					  }
//					 }
//				break;
//
//				default:
//					break;
//				}
//				ComparableNews comparator = new ComparableNews();
//				List<Item_News> data = new ArrayList<Item_News>(newList); 
//				Collections.sort(data, comparator);
//				newList = data;
//				
//			} catch (Exception e) {
//				Log.e("log_tag", "Error parsing data " + e.toString());
//			}
//		return newList;
//	}
//	public ArrayList<Item_News> getNewsOther() {
//		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//		JSONArray json = getNewsfromURL(GlobalVariable.URL_NEWS_ALL,nameValuePairs);
//		ArrayList<Item_News> newList = new ArrayList<Item_News>();
//		if (json != null) {
//			JSONArray one = json;
//
//			try {
//				for (int i = 0; i < one.length(); i++) {
//					
//					Item_News news = new Item_News();
//					JSONObject json_data = one.optJSONObject(i);
//					JSONObject new_data = json_data.getJSONObject("News");
//					news.setId(new_data.getString("id"));
//					news.setLink(new_data.getString("thumbnail_url"));
//					news.setAuthor(new_data.getString("author"));
//					news.setDesc(new_data.getString("summary"));
//					news.setTitle(new_data.getString("title"));
//					news.setRead_count(new_data.getString("read_count"));
//					news.setCategory_id(new_data.getString("category_id"));
//					newList.add(news);
//				}
//				for (int i = 0; i < 6; i++) {
//					newList.remove(i);
//				}
//			} catch (Exception e) {
//				Log.e("log_tag", "Error parsing data " + e.toString());
//			}
//
//		} else {
//			return null;
//		}
//		return newList;
//	}
//	public List<Item_News> getNewsLast2(Activity a) {
//		this.a = a;
//		List<Item_News> newList = new ArrayList<Item_News>();
//		List<Item_News> newList2 = new ArrayList<Item_News>();
//		try {
//			 RSSReader reader = new RSSReader();
//			 for(int i=1;i<10;i++){
//				  String uri = getUriNews(String.valueOf(i));
//				  RSSFeed feed = reader.load(uri);
//				  List<RSSItem> list = feed.getItems();
//
//				  for(int j=0;j<2;j++){
//					  	RSSItem r = list.get(j);
//						Item_News news = new Item_News();
//					    news.setTitle(r.getTitle());
//					    news.setLink(r.getLink().toString());
//					    news.setDesc(getImageSrc(r.getDescription()));
//					    news.setCategory(news_cat);
//					    news.setPubdate(calDiffDate(r.getPubDate()));
//					    news.setDiffdate(getDiffDay(r.getPubDate()));
//					    newList.add(news);
//				  }
//			 	}
//			 	ComparableNews comparator = new ComparableNews();
//				List<Item_News> data = new ArrayList<Item_News>(newList); 
//				Collections.sort(data, comparator);
//				newList = data;
//				if(newList!=null){
//					newList2.add(newList.get(0));
//					newList2.add(newList.get(1));
//				}
//		} catch (Exception e) {
//			Log.e("log_tag", "Error parsing data " + e.toString());
//		}
//	return newList2;
//	}
//	
//	public List<Item_News> getNewsSearch(Activity a,String criteria) {
//		this.a = a;
//		List<Item_News> newList = new ArrayList<Item_News>();
//		try {
//			 RSSReader reader = new RSSReader();
//			 for(int i=1;i<10;i++){
//				  String uri = getUriNews(String.valueOf(i));
//				  RSSFeed feed = reader.load(uri);
//				  List<RSSItem> list = feed.getItems();
//
//				  	for(RSSItem r : list){
//					  	if(r.getTitle().contains(criteria)){
//							Item_News news = new Item_News();
//						    news.setTitle(r.getTitle());
//						    news.setLink(r.getLink().toString());
//						    news.setDesc(getImageSrc(r.getDescription()));
//						    news.setCategory(news_cat);
//						    news.setPubdate(calDiffDate(r.getPubDate()));
//						    news.setDiffdate(getDiffDay(r.getPubDate()));
//						    newList.add(news);
//					  	}
//				  }
//			 	}
//			 	
//		} catch (Exception e) {
//			Log.e("log_tag", "Error parsing data " + e.toString());
//		}
//	return newList;
//	}
//	public Item_News getNewsById(String id) {
//		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//		nameValuePairs.add(new BasicNameValuePair("id", id));
//		JSONObject json = getNewsByIdfromURL(GlobalVariable.URL_NEWS_GET,nameValuePairs);
//		Item_News news = new Item_News();
//		if (json != null) {
//			try {
//					JSONObject newsJson = json.getJSONObject("News");
//					news.setId(newsJson.getString("id"));
//					news.setLink(newsJson.getString("thumbnail_url"));
//					news.setAuthor(newsJson.getString("author"));
//					news.setDesc(newsJson.getString("summary"));
//					news.setNews(newsJson.getString("news"));
//					news.setTitle(newsJson.getString("title"));
//					news.setRead_count(newsJson.getString("read_count"));
//					news.setCategory_id(newsJson.getString("category_id"));
//					JSONObject catJson = json.getJSONObject("NewsCategory");
//					news.setCategory(catJson.getString("category"));
//					
//				
//			} catch (Exception e) {
//				Log.e("log_tag", "Error parsing data " + e.toString());
//			}
//
//		} else {
//			return null;
//		}
//		return news;
//	}
//	
//	public boolean postStore_stats(StoreStatsDto dto) throws JSONException {
//		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//		nameValuePairs.add(new BasicNameValuePair("imei", dto.getImei()));
//		nameValuePairs.add(new BasicNameValuePair("weight", dto.getWeight()));
//		nameValuePairs.add(new BasicNameValuePair("height", dto.getHeight()));
//		nameValuePairs.add(new BasicNameValuePair("gender", dto.getGender()));
//		nameValuePairs.add(new BasicNameValuePair("age", dto.getAge()));
//		nameValuePairs.add(new BasicNameValuePair("bmi", dto.getBmi()));
//		
//		JSONObject json = postStat(GlobalVariable.URL_POST_STAT,nameValuePairs);
//		if(json!=null){
//			String status = json.getString("success");
//			if(!"false".equals(status)){
//				return true;
//			}else{
//				return false;
//			}
//		}else{
//			return false;
//		}
//		
//		
//	}

	public JSONArray getMagazinefromURL(String url,
			List<NameValuePair> nameValuePairs) {

		InputStream is = null;
		String result = "";
		JSONArray jArray = null;
		JSONObject jObject = null;
		try {
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
					HttpVersion.HTTP_1_1);
			HttpClient httpclient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				// System.out.println("Show Result::::"+line);
			}
			is.close();
			result = sb.toString();
			jObject = new JSONObject(result);
			
	
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		try {
			jArray = jObject.getJSONArray("magazines");
		} catch (Exception e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}
		return jArray;
	}
	public JSONArray getNewsfromURL(String url,
			List<NameValuePair> nameValuePairs) {

		InputStream is = null;
		String result = "";
		JSONArray jArray = null;
		JSONObject jObject = null;
		try {
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
					HttpVersion.HTTP_1_1);
			HttpClient httpclient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				// System.out.println("Show Result::::"+line);
			}
			is.close();
			result = sb.toString();
			jObject = new JSONObject(result);
			
	
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}

		try {
			jArray = jObject.getJSONArray("news");
		} catch (Exception e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}
		return jArray;
	}
	public JSONObject getNewsByIdfromURL(String url,
			List<NameValuePair> nameValuePairs) {

		InputStream is = null;
		String result = "";
		JSONArray jArray = null;
		JSONObject jObject = null;
		try {
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
					HttpVersion.HTTP_1_1);
			HttpClient httpclient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				// System.out.println("Show Result::::"+line);
			}
			is.close();
			result = sb.toString();
			jObject = new JSONObject(result);
			try {
				jArray = jObject.getJSONArray("news");
			} catch (Exception e) {
				Log.e("log_tag", "Error parsing data " + e.toString());
			}
	
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		return jObject;
	}
	public JSONArray getNewsByRss(String url,
			List<NameValuePair> nameValuePairs) {

		InputStream is = null;
		String result = "";
		JSONArray jArray = null;
		JSONObject jObject = null;
		try {
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
					HttpVersion.HTTP_1_1);
			HttpClient httpclient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				// System.out.println("Show Result::::"+line);
			}
			is.close();
			result = sb.toString();
			jObject = new JSONObject(result);
			
	
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		try {
			JSONObject jRss = jObject.getJSONObject("channel");
			jArray = jRss.getJSONArray("item");
		} catch (Exception e) {
			Log.e("log_tag", "Error parsing data " + e.toString());
		}
		return jArray;
	}
	public JSONObject postStat(String url,
			List<NameValuePair> nameValuePairs) {

		InputStream is = null;
		String result = "";
		JSONArray jArray = null;
		JSONObject jObject = null;
		try {
			HttpParams params = new BasicHttpParams();
			params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
					HttpVersion.HTTP_1_1);
			HttpClient httpclient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();
		} catch (Exception e) {
			Log.e("log_tag", "Error in http connection " + e.toString());
		}

		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					is, "iso-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
				// System.out.println("Show Result::::"+line);
			}
			is.close();
			result = sb.toString();
			jObject = new JSONObject(result);
			
	
		} catch (Exception e) {
			Log.e("log_tag", "Error converting result " + e.toString());
		}
		return jObject;
	}
	public String getImageSrc(String s){
		String word0= "";
		if(s != null && !"".equals(s)){
		Pattern p=null;
		Matcher m= null;
		
		String word1= null;
		p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
		m = p.matcher(s);
		if (m.find()) {
			word0 = m.group(1);
		}
		}
		return word0;
	}
//	public String getUriNews(String type){
//		String uri = "";
//		if(type!=null){
//		int t = Integer.parseInt(type);
//		switch (t) {
//		case 1:
//			uri = "http://www.dpe.go.th/rss/news/dpe_adv_news.xml";
//			news_cat = a.getResources().getString(R.string.news_adv);
//			break;
//		case 2:
//			uri = "http://www.dpe.go.th/rss/news/dpe_news.xml";
//			news_cat = a.getResources().getString(R.string.news_dpe);
//			break;
//		case 3:
//			uri = "http://www.dpe.go.th/rss/news/dpe_sport_news.xml";
//			news_cat = a.getResources().getString(R.string.news_sport);
//			break;
//		case 4:
//			uri = "http://www.dpe.go.th/rss/news/dpe_rec_news.xml";
//			news_cat = a.getResources().getString(R.string.news_rec);
//			break;
//		case 5:
//			uri = "http://www.dpe.go.th/rss/news/dpe_jud_news.xml";
//			news_cat = a.getResources().getString(R.string.news_jud);
//			break;
//		case 6:
//			uri = "http://www.dpe.go.th/rss/news/dpe_bid_news.xml";
//			news_cat = a.getResources().getString(R.string.news_bid);
//			break;
//		case 7:
//			uri = "http://www.dpe.go.th/rss/news/dpe_sci_news.xml";
//			news_cat = a.getResources().getString(R.string.news_sci);
//			break;
//		case 8:
//			uri = "http://www.dpe.go.th/rss/news/dpe_reg_news.xml";
//			news_cat = a.getResources().getString(R.string.news_reg);
//			break;
//		case 9:
//			uri = "http://www.dpe.go.th/rss/news/dpe_mov_news.xml";
//			news_cat = a.getResources().getString(R.string.news_mov);
//			break;
//		case 10:
//			uri = "http://www.dpe.go.th/rss/news/dpe_stadium_news.xml";
//			news_cat = a.getResources().getString(R.string.news_stadium);
//			break;
//
//		default:
//			break;
//		}
//		}	
//		return uri;	
//	}
	public long getDiffDay(Date d){
		 long diffDays = 0L;
		if(d!=null){
			Calendar calendar1 = Calendar.getInstance();
		    Calendar calendar2 = Calendar.getInstance();
		    calendar1.set(calendar1.get(Calendar.YEAR), d.getMonth(), d.getDate());
		    long milliseconds1 = calendar1.getTimeInMillis();
		    long milliseconds2 = calendar2.getTimeInMillis();
		    long diff = milliseconds2 - milliseconds1;
		    diffDays = diff / (24 * 60 * 60 * 1000);
		}
		return diffDays;
	}
	
	public String calDiffDate(Date d){
		String diffResultDt = "";
		if(d!=null){
			Calendar calendar1 = Calendar.getInstance();
		    Calendar calendar2 = Calendar.getInstance();
		    calendar1.set(calendar1.get(Calendar.YEAR), d.getMonth(), d.getDate());
		    long milliseconds1 = calendar1.getTimeInMillis();
		    long milliseconds2 = calendar2.getTimeInMillis();
		    long diff = milliseconds2 - milliseconds1;
		    long diffMinutes = diff / (60 * 1000);
		    long diffHours = diff / (60 * 60 * 1000);
		    long diffDays = diff / (24 * 60 * 60 * 1000);
		    long diffMon = diffDays/30;
	
		    
		    if(diffMon>0){
		    	diffResultDt = diffMon +a.getResources().getString(R.string.diff_month);
		    }
		    else if(diffDays>0){
		    	diffResultDt = diffDays +a.getResources().getString(R.string.diff_day);
		    }else if(diffHours > 0){
		    	diffResultDt = diffHours +a.getResources().getString(R.string.diff_hour);
		    }else if(diffMinutes > 0){
		    	diffResultDt = diffMinutes +a.getResources().getString(R.string.diff_min);
		    }
		}
		return diffResultDt;
	}
}